//
//  LLYTSmallUtil.m
//  huafeibao
//
//  Created by xuyf on 13-6-6.
//  Copyright (c) 2013年 ZPP. All rights reserved.
//

#import "LLSmallUtil.h"

@implementation LLYTSmallUtil

+ (int)iOSBigVersion
{
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    
    int bigVersion = (int)version;
    
    return bigVersion;
}

+ (CGFloat)tableCutHeight
{
    if ([self iOSBigVersion] < 7) {
        return 44;
    }
    
    return 64;
}

+(BOOL)hasNextPage:(NSDictionary *)dic
{
    int pageNo = [dic[@"pageNo"] intValue];
    int pageSize = [dic[@"pageSize"] intValue];
    int totalSize = [dic[@"totalSize"] intValue];
    if (pageNo*pageSize < totalSize) {
        return YES;
    } else {
        return NO;
    }
}

+(NSString *) getNextPageNo:(NSString *)currentPageNo
{
    int pageNo = [currentPageNo intValue];
    return [NSString stringWithFormat:@"%i", pageNo++];
}
@end

@implementation UIColor(CreatorAdditon)

+ (UIColor*)colorWithR:(float)r G:(float)g B:(float)b A:(float)a
{
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a];
}

+ (UIColor*)colorWithHTMLStr:(NSString*)htmlRGBString
{
    if ([htmlRGBString rangeOfString:@","].location != NSNotFound) {
        return [UIColor colorWithRGB:htmlRGBString];
    }
    
    NSDictionary *colorDic = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"#00FFFF",@"aqua",
                              @"#000000",@"black",
                              @"#0000FF",@"blue",
                              @"#FF00FF",@"fuchsia",
                              @"#808080",@"gray",
                              @"#008000",@"green",
                              @"#00FF00",@"lime",
                              @"#800000",@"maroon",
                              @"#000080",@"navy",
                              @"#808000",@"olive",
                              @"#FFA500",@"orange",
                              @"#800080",@"purple",
                              @"#FF0000",@"red",
                              @"#C0C0C0",@"silver",
                              @"#008080",@"teal",
                              @"#FFFFFF",@"white",
                              @"#FFFF00",@"yellow",
                              @"", @"clear",
                              nil];
    
    if ([colorDic objectForKey:htmlRGBString] != nil)
    {
        htmlRGBString = [colorDic objectForKey:htmlRGBString];
    }
    
	if ([htmlRGBString length] == 0) {
		return [UIColor clearColor];
	}
    
    unsigned int r = 0;
    unsigned int g = 0;
    unsigned int b = 0;
    unsigned int a = 255;
    
    if ([htmlRGBString rangeOfString:@"#"].location != NSNotFound) 
    {
        htmlRGBString = [htmlRGBString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    }
    
    NSScanner *scanner = nil;
    
    if (htmlRGBString.length >= 6) {
        
        scanner = [NSScanner scannerWithString:[htmlRGBString substringWithRange:NSMakeRange(0, 2)]];
        [scanner scanHexInt:&r];
        
        scanner = [NSScanner scannerWithString:[htmlRGBString substringWithRange:NSMakeRange(2, 2)]];
        [scanner scanHexInt:&g];
        
        scanner = [NSScanner scannerWithString:[htmlRGBString substringWithRange:NSMakeRange(4, 2)]];
        [scanner scanHexInt:&b];
        
        if (htmlRGBString.length == 8) {
            scanner = [NSScanner scannerWithString:[htmlRGBString substringWithRange:NSMakeRange(6, 2)]];
            [scanner scanHexInt:&a];
        }
    }
    else
    {
        //        scanner = [NSScanner scannerWithString:[htmlRGBString substringWithRange:NSMakeRange(0, 1)]];
        //        [scanner scanHexInt:&r];
        //        
        //        scanner = [NSScanner scannerWithString:[htmlRGBString substringWithRange:NSMakeRange(1, 1)]];
        //        [scanner scanHexInt:&g];
        //        
        //        scanner = [NSScanner scannerWithString:[htmlRGBString substringWithRange:NSMakeRange(2, 1)]];
        //        [scanner scanHexInt:&b];
    }
    
    return [UIColor colorWithR:r G:g B:b A:a/255.0];
}

// 12,22,33 含两个逗号
+ (UIColor*)colorWithRGB:(NSString*)rgbString
{
    NSArray *colorArray = [rgbString componentsSeparatedByString:@","];
    
    if (colorArray.count < 3) {
        return [UIColor redColor];
    }
    
    unsigned int r = [colorArray[0] intValue];
    unsigned int g = [colorArray[1] intValue];
    unsigned int b = [colorArray[2] intValue];
    unsigned int a = 255;
    
    if (colorArray.count > 3) {
        a = [colorArray[1] intValue];
    }
    
    return [UIColor colorWithR:r G:g B:b A:a/255.0];
}
@end



@implementation UIButton (easyAccess)

- (void)setLongSelector:(SEL)selector withTarget:(id)target
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:target action:selector];
    longPress.minimumPressDuration = 0.8; //定义按的时间
    [self addGestureRecognizer:longPress];
}

- (void)setSelector:(SEL)selector withTarget:(id)target
{
    [self addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
}

- (void)setTitle:(NSString*)title
{
    [self setTitle:title forState:UIControlStateNormal];
}

- (void)setImage:(UIImage *)image
{
    [self setImage:image forState:UIControlStateNormal];
}

- (void)setBackgroundImage:(UIImage *)image
{
    [self setBackgroundImage:image forState:UIControlStateNormal];
}

- (void)centerImageAndTitle:(float)spacing  
{      
    // get the size of the elements here for readability  
    CGSize imageSize = self.imageView.frame.size;  
    CGSize titleSize = self.titleLabel.frame.size;  
    
    if ([LLYTSmallUtil iOSBigVersion] > 6) {
        titleSize = [[self titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    }
    
    // get the height they will take up as a unit  
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);  
    
    // raise the image and push it right to center it  
    self.imageEdgeInsets = UIEdgeInsetsMake(  
                                            - (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);  
    
    // lower the text and push it left to center it  
    self.titleEdgeInsets = UIEdgeInsetsMake(  
                                            0.0, - imageSize.width, - (totalHeight - titleSize.height), 0.0);      
}  

- (void)centerImageAndTitle  
{  
    const int DEFAULT_SPACING = 1.0f;  
    [self centerImageAndTitle:DEFAULT_SPACING];  
}  

@end

@implementation UIImage (SDKBundle)

+ (UIImage*)sdkBundleImageNamed:(NSString *)imageName
{
    UIImage *image = nil;
    
    image = [UIImage imageNamed:imageName];
    
    return image;
}

@end

@implementation UIImageView (easyAccess)

+ (UIImageView*)imageViewWithName:(NSString*)imageName
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage sdkBundleImageNamed:imageName]];
    
    return imageView;
}

@end
//@implementation UITextField (delegate)
//- (UITextField*)initWithFrame:(CGRect)rect delegate:(id)delegate{
//    
//}
//@end

@implementation UIView (Alignment)

- (void)adjustViewLineCenter:(UIView*)subwidget
{
    subwidget.left = (self.width - subwidget.width)/2;
}

@end

@implementation NSObject (json)

- (NSString*)jsonString
{
    NSError *error = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                 encoding:NSUTF8StringEncoding];
    
    return jsonString;
}

@end